using System;
using System.Drawing;
using System.Windows.Forms;

namespace PopLava.Tools
{
    /// <summary>
    /// Allows the user to click and drag controls around
    /// </summary>
    public class DragControl
    {
        private Control g_oControl;
        private bool g_bCreatingNewTile;
        private int g_iX;
        private int g_iY;

        /// <summary>
        /// Pass in the control to be dragged
        /// </summary>
        public DragControl(Control control)
        {
            this.g_oControl = control;
        }
        /// <summary>
        /// When the user clicks and holds most anywhere on the form, we will start recording
        /// the mouse position
        /// </summary>
        public void GrabControl(int x, int y)
        {
            g_iX = x;
            g_iY = y;
        }
        /// <summary>
        /// While the mouse has the left mouse button down, we will move the object.
        /// </summary>
        public void MoveControl(System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left || g_bCreatingNewTile)
            {
                Point pLocation = new System.Drawing.Point();
                pLocation.X = this.g_oControl.Location.X + (e.X - g_iX);
                pLocation.Y = this.g_oControl.Location.Y + (e.Y - g_iY);
                if (this.g_oControl.Location != pLocation)
                {
                    this.g_oControl.Location = pLocation;
                }
            }
        }
        /// <summary>
        /// When the user releases the left mouse button we will stop moving the control.
        /// </summary>
        public void ReleaseControl(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                g_bCreatingNewTile = false;
            }
        }
    }
}
